package ModelLayer;
import java.util.ArrayList;
/**
 * Container for item objects
 * 
 * @author (Gruppe 3)
 */
public class StorageContainer
{
    private ArrayList<Item> items;
    private static StorageContainer instance;
    
    /**
     * constructor
     */
    private StorageContainer()
    {
        items = new ArrayList<Item>();
    }
    
    /**
     * Singleton
     */ 
    public static StorageContainer getInstance()
    {
        if(instance == null)
        {
            instance = new StorageContainer();
        }
        return instance;
    }
    
    /**
     * Adds an item to the the ArrayList items  
     * @param itemCode  id / code of the item
     * @param name      name of the item
     * @param type      which type is the item (example: Hammer)
     * @param price     what price the item is being sold at (example: 100)
     * @param discount  how many procent of the price will taken away(example: 20% = 0.20)
     */   
    public void addItem(String itemCode, String name, String type, double price, double discount)
    {
        if(itemCode != null && name != null && type != null && price >= 0.0  && discount >= 0.0)
        {
            Item newItem = new Item(itemCode, name, type, price, discount);
            items.add(newItem);
        }
    }
    
    /**
     * Checks for a wanted item in the ArrayList items  
     * @param searchItemCode id / code of the wanted item
     * @return if wanted item is found, it is returned, otherwise it returns null
     */
    public Item findItem(String searchItemCode)
    {
        for(int x = 0; x < items.size(); x++)
        {
            if(items.get(x).getItemCode().equalsIgnoreCase(searchItemCode))
            {
                return items.get(x);
            }
        }
        return null;
    }
    
    /**
     * Deletes a wanted item in the ArrayList items 
     * @param  searchItemCode the id / code of the wanted item
     */
    public void deleteItem(String searchItemCode)
    {   
       int x = 0;
       if (findItem(searchItemCode) != null)
       {
           items.remove(x);
       }
    }
        
    /**
     * Updates an item in the ArrayList items
     * If the specific update input parameter is emptry, it doesnt change the field
     * @param oldItemCode   old id / code of the wanted item
     * @param itemCode      new id / code of the wanted item    
     * @param name          new name of the item
     * @param type          new type of the item (example: Hammer)
     * @param price         new price of the item, what it will be sold at (example: 100)
     * @param discount      new discount of item, how many procent of the price will taken away(example: 20% = 0.20)    
     */
    public void updateItem(String oldItemCode, String itemCode, String name, String type, double price, double discount )
    {
        for (int x = 0; x < items.size(); x++)
        {
            if (items.get(x).getItemCode().equalsIgnoreCase(oldItemCode))
            {   
                if (itemCode != null)
                {
                    items.get(x).setItemCode(itemCode);
                }
                
                if (name != null)
                {
                    items.get(x).setName(name);
                }
                
                if (type != null)
                {
                    items.get(x).setType(type);
                }
                
                if (price > 0.0)
                {
                    items.get(x).setPrice(price);
                }
                
                if (discount > 0.0)
                {
                    items.get(x).setDiscount(discount);
                }
            }
        }
    }
    
    public void listItem(String searchItemCode)
    {
        Item foundItem = findItem(searchItemCode);
        
        System.out.println("ItemCode: " + foundItem.getItemCode());
        System.out.println("Name: " + foundItem.getName());
        System.out.println("Type: " + foundItem.getType());
        System.out.println("Price: " + foundItem.getPrice());
        // for future development
        //System.out.println("Discount: " + foundItem.getDiscount());
        System.out.println("");
        
    }
    
    /**
     * prints all item i of ArrayList items in the storageContainer as output to the console
     */
    public void listAllItems()
    {
        for(Item i : items)
        {
            System.out.println("ItemCode: " + i.getItemCode());
            System.out.println("Name: " + i.getName());
            System.out.println("Type: " + i.getType());
            System.out.println("Price: " + i.getPrice());
            // for future development
            //System.out.println("Discount: " + i.getDiscount());
            System.out.println();
        }
        
    }
    
    public Item getItem(String searchItemCode)
    {   
        Item foundItem = findItem(searchItemCode);
        if (foundItem != null)
        {
            return foundItem;
        }
        
        return null;
    }
}
        